import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_reader/model/home/HotModel.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:nice_button/generated/i18n.dart';
import 'package:flutter_reader/tools/globals.dart' as globals;
import 'package:overlay_support/overlay_support.dart';
import 'package:permission_handler/permission_handler.dart';

class ConnectUsPage extends StatefulWidget {
  ConnectUsPage({Key key}) : super(key: key);

  @override
  _ConnectUsPageState createState() {
    return _ConnectUsPageState();
  }
}

class _ConnectUsPageState extends State<ConnectUsPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('联系客服'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 4,
              child: Container(
                child: Image(
                  fit: BoxFit.fitWidth,
                  width: ScreenUtil().setWidth(700),
                  image: NetworkImage(globals.qrCode),
                ),
              ),
            ),
            Expanded(
              flex: 2,
              child: InkWell(
                onTap: _save,
                child: Container(
                  width: ScreenUtil().setWidth(925),
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(100),right: ScreenUtil().setWidth(100)),
                  decoration: BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black38,
                          blurRadius: 10.0,
                          offset: Offset(3.0, 3.0),),
                      ],
                      gradient: LinearGradient(
                        colors: <Color>[
                          Color(0xfffdfcfb),
                          Color(0xffe2d1c3)
                        ],
                      ),
                      borderRadius: BorderRadius.all(Radius.circular(10))
                  ),
                  child: Container(
                    padding: EdgeInsets.only(top: ScreenUtil().setHeight(100)),
                    child: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Container(
                            width: ScreenUtil().setWidth(725),
                            child: FittedBox(
                              fit: BoxFit.fitWidth,
                              child: Text(
                                  "客服微信：  kanxiaoshuo101",
                                style: TextStyle(
                                  color: Colors.black38
                                ),
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(100)),
                            width: ScreenUtil().setWidth(725),
                            child: FittedBox(
                              fit: BoxFit.fitWidth,
                              child: Text(
                                  "或点击此处保存二维码到本地相册",
                                style: TextStyle(
                                  color: Colors.black54
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              flex: 2,
              child: Container(

              ),
            )
          ],
        ),
      ),
    );
  }

  _save() async {

    if (Platform.isAndroid) {
      PermissionStatus permissionStorage =
      await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
      if (permissionStorage != PermissionStatus.granted) {
        Map<PermissionGroup, PermissionStatus> permissionStatus =
        await PermissionHandler().requestPermissions([PermissionGroup.storage]);
        permissionStorage = permissionStatus[PermissionGroup.storage] ?? PermissionStatus.unknown;

        if (permissionStorage != PermissionStatus.granted) {
          print("❌----------has no Permission");
          return;
        }
      }
    }

    var response = await Dio().get(globals.qrCode, options: Options(responseType: ResponseType.bytes));
    final result = await ImageGallerySaver.saveImage(Uint8List.fromList(response.data));
    print(result);
    if(result.toString() != null){
      toast("已保存到本地相册");
    }
  }
}